import axios from '../../extend/axios'
import { ActionContext, ActionTree, GetterTree, MutationTree } from 'vuex'

interface UserInfo {
  token: string
  roleType: string
  userId: string
  userStatus: string
  username: string
}

class State {
  public userInfo: UserInfo = {
    token: '',
    roleType: '',
    userId: '',
    userStatus: '',
    username: ''
  }
}

const mutations = {
  setUserInfo (state: State, res: AjaxResponse) {
    state.userInfo = res.data
    window.sessionStorage.setItem('userInfo', JSON.stringify(res.data))
    window.sessionStorage.setItem('token', res.data.token)
  },
  removeUserInfo (state: State) {
    state.userInfo = {
      token: '',
      roleType: '',
      userId: '',
      userStatus: '',
      username: ''
    }
    window.sessionStorage.clear()
  }
} as MutationTree<State>
const actions = {} as ActionTree<State, any>
const getters = {
  userInfo (state: State) {
    if (state.userInfo.username) {
      return state.userInfo
    } else {
      return JSON.parse(window.sessionStorage.getItem('userInfo') || '')
    }
    // return JSON.parse(window.sessionStorage.getItem('userInfo') || '')
  }
} as GetterTree<State, any>
const user = {
  namespace: true,
  state: new State(),
  mutations,
  actions,
  getters
}
export default user
/**
 * 0.admin
 * 1.大掌柜
 *
 * 2.客服
 *
 * 3.渠道专员  6.官方渠道专员
 *
 * 4.渠道业务员  8.官方渠道业务员
 *
 * 5.渠道商(b2b, 银行) 7.官方渠道商
 * 9.企业会员
 */
